package cn.com.inossem.operation;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;

public class YanZhengmaOperation implements Operation {
	public static final String YANZHENGMA_OPERATION_KEY = "yanzhengma_operation_key";
	public static final String YANZHENGMA_OPERATION_VALUE = "yanzhengma_operation_value";
	public static final String YANZHENGMA_OPERATION_USERTYPE = "yanzhengma_operation_usertype";
	public static Bundle bundle;

	Context content = null;

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {
		content = arg0;

		String key = request.getString(YANZHENGMA_OPERATION_KEY);
		String value = request.getString(YANZHENGMA_OPERATION_VALUE);
		String usertype = request.getString(YANZHENGMA_OPERATION_USERTYPE);

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "login/sendValidcode");
		connection.setPostText("{\"" + key + "\":\""
				+ value.replace("\n", "").replace("\r", "") + "\"}");

		connection.setPostText("{\"" + key + "\":\""
				+ value.replace("\n", "").replace("\r", "") + "\",\""
				+ "usetype" + "\":\"" + usertype + "\"}");

		System.out.println("{\"" + key + "\":\""
				+ value.replace("\n", "").replace("\r", "") + "\",\""
				+ "usetype" + "\":\"" + usertype + "\"}");

		String body = connection.execute();

		System.out.println("YanZhengmaOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");

			if (errorFlag == WSConfig.RESULT_SUCESS) {
				System.out.print("修改成功!");
			} else {

			}

			bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS,
					errorFlag);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}

	public void charu(String tabname, ContentValues contentValues,
			String whereClause) {
		DatabaseHelper dh = new DatabaseHelper(content);
		SQLiteDatabase db = dh.getWritableDatabase();

		db.update(tabname, contentValues, whereClause, null);

		db.close();
		dh.close();
	}
}
